//
//  其它影像学报告组
//  yilutongxing
//
//  Created by T&J on 2017/12/7.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "ImageReportArrayCell.h"
#import "DzyImgPicker.h"
#import <Masonry.h>
#import "OtherImageReportArrayViewController.h"

@interface ImageReportArrayCell()<DzyImgDelegate, UITextViewDelegate>
@property (strong, nonatomic) NSMutableArray *imgview_arr;
@property (strong, nonatomic) DzyImgPicker *picker;
@property (strong, nonatomic) NSIndexPath *current_indexpath;
@end

@implementation ImageReportArrayCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.desc_textview.delegate = self;
    
    [self.title_textfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateConstraints{
    [self.picker.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.picker);
        make.left.equalTo(self.picker);
        make.bottom.equalTo(self.picker);
        make.right.equalTo(self.picker);
    }];
    [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.image_bg);
        make.left.equalTo(self.image_bg);
        make.bottom.equalTo(self.image_bg);
        make.right.equalTo(self.image_bg);
    }];
    [super updateConstraints];
}

- (void)refreshIndex:(NSIndexPath*)indexPath{
    self.current_indexpath = indexPath;
}

- (void)setupImagePicker:(UIViewController*)parentVC{
    if (!_picker) {
        self.picker = [[DzyImgPicker alloc]initWithFrame:CGRectMake(0, 5, self.image_bg.frame.size.width, self.image_bg.frame.size.height)
                                              andParentV:parentVC
                                               andMaxNum:3];
        self.picker.dzyImgDelegate = self;
        [self.image_bg addSubview:self.picker];
    }
}

- (void)setDesc:(NSString*)desc{
    if ([desc isNotEmpty]) {
        self.desc_placeholder_label.text = @"";
    }
    self.desc_textview.text = desc;
}

- (void)fillImages:(NSArray *)img_arr{
    if (_picker) {
        [_picker setImages:img_arr];
    }
}

- (void)fillImagesForUrl:(NSString *)urlStr{
    NSMutableArray *urlArr = [[urlStr componentsSeparatedByString:@","] mutableCopy];
    NSEnumerator *reveren = [urlArr reverseObjectEnumerator];
    NSString *url = nil;
    while (url = [reveren nextObject]) {
        if (![url isNotEmpty]) {
            [urlArr removeObject:url];
        }
    }
    static NSMutableArray *img_arr = nil;
    img_arr = [NSMutableArray array];
    for (int i=0; i<urlArr.count; i++) {
        UIImageView *imgv = [[UIImageView alloc]init];
        [self.imgview_arr addObject:imgv];//缓存控件，防止被delete导致图片请求无返回
        [imgv sd_setImageWithURL:[NSURL URLWithString:urlArr[i]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                [img_arr addObject:image];
                if (img_arr.count==urlArr.count){
                    [self.picker setImages:img_arr];
                    if(self.delegate && [self.delegate respondsToSelector:@selector(sendImgArr:forIndexPath:)]) {
                        [self.delegate sendImgArr:img_arr forIndexPath:self.current_indexpath];
                    }
                }
            }
        }];
    }
}

#pragma mark - DzyImgDelegate
- (void)getImages:(NSArray *)imgData{
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendImgArr:forIndexPath:)]) {
        [self.delegate sendImgArr:imgData forIndexPath:self.current_indexpath];
    }
}
#pragma mark - 文本控件内容获取
- (void)textFieldDidChange:(UITextField*)textfield{
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendTitle:forIndexPath:)]) {
        [self.delegate sendTitle:textfield.text forIndexPath:self.current_indexpath];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    if ([textView.text isNotEmpty]) {
        self.desc_placeholder_label.text = @"";
    }
    else {
        self.desc_placeholder_label.text = @"请在此输入影像报告的文字说明";
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendDesc:forIndexPath:)]) {
        [self.delegate sendDesc:textView.text forIndexPath:self.current_indexpath];
    }
}

#pragma mark - 数据配置
- (NSMutableArray *)imgview_arr{
    if (!_imgview_arr) {
        _imgview_arr = [NSMutableArray array];
    }
    return _imgview_arr;
}

@end
